{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.4.5 The JMP Instruction\n", "\n", "The conditional branch instruction, for all its capability, does have one unfortunate\n", "limitation. The next instruction executed must be within the range of addresses\n", "that can be computed by adding the incremented PC to the sign-extended offset obtained from bits [8:0] of the instruction. Since bits [8:0] specify a 2's complement\n", "integer, the next instruction executed after the conditional branch can be\n", "at most +256 or —255 locations from the branch instruction itself. What if we\n", "would like to execute next an instruction that is 1,000 locations from the current\n", "instruction. We cannot fit the value 1,000 into the 9-bit field; ergo, the conditional\n", "branch instruction does not work.\n", "\n", "The LC-3 ISA does provide an instruction JMP (opcode = 1100) that can\n", "do the job. An example follows: \n", "\n", "15 14 13 12 | 11 10 9 | 8 7 6 | 5 4 3 2 1 0\n", "-------- | ------ |------ |------------\n", "1 1 0 0 | 0 0 0 | 0 1 0 | 0 0 0 0 0 0\n", "JMP | | R2 | \n", "\n", "The JMP instruction loads the PC with the contents of the register specified by\n", "bits [8:6] of the instruction. If this JMP instruction is located at address x4000,\n", "R2 contains the value x6600, and the PC contains x4000, then the instruction at\n", "x4000 (the JMP instruction) will be executed, followed by the instruction located\n", "at x6600. Since registers contain 16 bits, the full address space of memory, the\n", "JMP instruction has no limitation on where the next instruction to be executed\n", "must reside. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# TRAP\n", "\n", "## 9.1 LC-3 TRAP Routines\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The message is this: If a value in a register will be needed after something\n", "else is stored in that register, we must save it before the something else happens\n", "and restore it before we can subsequently use it. We save a register value\n", "by storing it in memory; we restore it by loading it back into the register.\n", "\n", "The save/restore problem can be handled either by the initiating program\n", "before the TRAP occurs or by the called program (for example, the service routine)\n", "after the TRAP instruction executes. We will see in Section 9.2 that the\n", "same problem exists for another class of calling/called programs, the subroutine\n", "mechanism.\n", "\n", "We use the term **caller-save** if the calling program handles the problem.\n", "We use the term **callee-save** if the called program handles the problem. The\n", "appropriate one to handle the problem is the one that knows which registers will\n", "be destroyed by subsequent actions. \n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.2 Subroutines\n", "\n", "**Sidebar**: TRAP routines are only slightly different from subroutines. TRAP routines deal with permission to restricted hardware, such as a monitor, keyboard, etc. We won't dive any further into this *privilege* in this course. Take Operating Systems for further information on that topic.\n", "\n", "Also, one might require a fragment that has been supplied by the manufacturer\n", "or by some independent software supplier. It is almost always the case that\n", "collections of such fragments are available to user programmers to free them from\n", "having to write their own. These collections are referred to as libraries. An example\n", "is the Math Library, which consists of fragments that execute such functions\n", "as **square root**, **sine**, and **arctangent**.\n", "\n", "For all of these reasons, it is good to have a way to use program fragments\n", "efficiently. Such program fragments are called subroutines, or alternatively, procedures,\n", "or in C terminology, functions. The mechanism for using them is referred\n", "to as a Call/Return mechanism. \n", "\n", "\n", "\n", "### 9.2.2 JSR/JSRR\n", "\n", "Opcode | Mode Flag | Operands\n", "-------| --------- | ---------\n", "0100 | 1 | PC offset11\n", "0100 | 0 | 00 REG 000000\n", "\n", "Modes:\n", "\n", "* 1 - PC + offset is location of subroutine\n", "* 0 - REG is location of subroutine" ] } ], "metadata": { "kernelspec": { "display_name": "Calysto LC3", "language": "asm", "name": "calysto_lc3" }, "language_info": { "file_extension": ".asm", "mimetype": "text/x-gas", "name": "gas" } }, "nbformat": 4, "nbformat_minor": 2 }